<template>
  <div>
    <el-dialog
      title="添加文章分类"
      :visible.sync="dialogVisible"
      width="30%">
      <el-form ref="addForm" :model="addform" :rules="addformRules" label-width="80px">
        <el-form-item label="分类名称" prop="cate_name">
          <el-input v-model="addform.cate_name"></el-input>
        </el-form-item>
        <el-form-item label="分类别名" prop="cate_alias">
          <el-input v-model="addform.cate_alias"></el-input>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="dialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="SaddSubmit">确 定</el-button>
      </span>
    </el-dialog>
    <el-dialog
      title="修改文章分类"
      :visible.sync="editVisible"
      width="30%">
      <el-form ref="editForm" :model="editForm" :rules="editformRules" label-width="80px">
        <el-form-item label="分类名称" prop="cate_name">
          <el-input v-model="editForm.cate_name"></el-input>
        </el-form-item>
        <el-form-item label="分类别名" prop="cate_alias">
          <el-input v-model="editForm.cate_alias"></el-input>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="editVisible = false">取 消</el-button>
        <el-button type="primary" @click="SeditSubmit">确 定</el-button>
      </span>
    </el-dialog>
    <el-card class="box-card">
      <div slot="header" class="clearfix header-box">
        <span>文章分类</span>
        <el-button type="primary" size="mini" @click="dialogVisible=true">添加分类</el-button>
      </div>
       <!-- 文章列表区域 -->
       <el-table
        border
        :data="cateList"
        style="width: 100%">
        <el-table-column
          label="序号"
          width="180"
          type="index">
        </el-table-column>
        <el-table-column
          label="分类名称"
          width="180"
          prop="cate_name">

        </el-table-column>
        <el-table-column
          label="分类别名"
          width="180"
          prop="cate_alias">

        </el-table-column>
        <el-table-column label="操作">
          <template v-slot='{row}'>
            <el-button
              size="mini"
              type="primary"
              @click="editCateList(row)">编辑</el-button>
            <el-button
              size="mini"
              type="danger"
              @click="delCateList(row.id)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
    </el-card>
  </div>
</template>

<script>
export default {
  name: 'ArtCate',
  data () {
    return {
      dialogVisible: false,
      editVisible: false,
      cateList: [],
      addform: {
        cate_name: '',
        cate_alias: ''
      },
      addformRules: {
        cate_name: [
          { required: true, message: '请输入分类名', trigger: 'blur' },
          { pattern: /^\S{1,9}$/, message: '请输入1-9位非空字符', trigger: 'blur' }
        ],
        cate_alias: [
          { required: true, message: '请输入分类名', trigger: 'blur' },
          { pattern: /^\S{1,9}$/, message: '请输入1-9位非空字符', trigger: 'blur' }
        ]
      },
      editForm: {

      },
      editformRules: {
        cate_name: [
          { required: true, message: '请输入分类名', trigger: 'blur' },
          { pattern: /^\S{1,9}$/, message: '请输入1-9位非空字符', trigger: 'blur' }
        ],
        cate_alias: [
          { required: true, message: '请输入分类名', trigger: 'blur' },
          { pattern: /^\S{1,9}$/, message: '请输入1-9位非空字符', trigger: 'blur' }
        ]
      }
    }
  },
  // 先发送ajax请求数据
  methods: {
    async upCateList () {
      const { data: res } = await this.$axios.get('/my/cate/list')
      if (res.code !== 0) return this.$message.success(res.message)
      this.cateList = res.data
    },
    SaddSubmit () {
      this.$refs.addForm.validate(async valid => {
        console.log(valid)
        if (!valid) return
        const { data: res } = await this.$axios.post('/my/cate/add', this.addform)
        if (res.code !== 0) return this.$message.error(res.message)
        this.$message.success(res.message)
        this.upCateList()
        this.$refs.addForm.resetFields()
        this.dialogVisible = false
      })
    },
    async delCateList (id) {
      const { data: res } = await this.$axios.delete('/my/cate/del', { params: { id } })
      console.log(res)
      if (res.code !== 0) return this.$message.error(res.message)
      this.$message.success(res.message)
      this.upCateList()
    },
    SeditSubmit () {
      this.$refs.editForm.validate(async valid => {
        console.log(valid)
        if (!valid) return
        const { data: res } = await this.$axios.put('/my/cate/info', this.editForm)
        if (res.code !== 0) return this.$message.error(res.message)
        this.$message.success(res.message)
        this.upCateList()
        this.editVisible = false
      })
    },
    editCateList (row) {
      this.editForm = { ...row }
      // 在这里不能直接赋值，用户体验效果不好可以进行一次深拷贝。
      this.editVisible = true
    }
  },
  created () {
    this.upCateList()
  }
}
</script>

<style lang="less" scoped>
.header-box {
  display: flex;
  justify-content: space-between;
  align-items: center;
}
</style>
